package com.zhiche.lisa.lspm.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import com.zhiche.lisa.lspm.dao.model.driver.bo.DriverLineBO;
import com.zhiche.lisa.lspm.dto.DriverTypeDTO;
import com.zhiche.lisa.lspm.service.driver.IDriverService;
import com.zhiche.lisa.lspm.service.msg.SendMsgService;
import com.zhiche.lisa.lspm.vo.driver.DriverLineVO;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * User: suohao
 * Date: 2019/5/30
 */
@RestController
@RequestMapping(value = "/sendMessage")
public class SendMessageController {
    private static final Logger LOGGER = LoggerFactory.getLogger(DriverController.class);


    @Autowired
    private  IDriverService iDriverService;

    @Autowired
    private SendMsgService sendMsgService;


    @PostMapping(value = "/driverList")
    @ApiOperation(value = "获取司机列表", notes = "获取司机列表", produces = MediaType.APPLICATION_JSON_VALUE,
            response = RestfulResponse.class)
    public RestfulResponse<Page<DriverTypeDTO>> getDriverList(@RequestBody Page<DriverTypeDTO> page) {
        LOGGER.info("SendMessageController.driverList（获取司机类型列表） params : {} ", page);

        RestfulResponse<Page<DriverTypeDTO>> result = new RestfulResponse<>(CommonEnum.SUCCESS
                .getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<DriverTypeDTO> pageVo = iDriverService.getDriverList(page);
            result.setData(pageVo);
        } catch (BaseException e) {
            LOGGER.error("SendMessageController.driverList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("SendMessageController.driverList ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }

    @PostMapping(value = "/sendMsg")
    @ApiOperation(value = "发送短信", notes = "发送短信", produces = MediaType.APPLICATION_JSON_VALUE,
            response = RestfulResponse.class)
    public RestfulResponse<String> sendMsg(@RequestBody Map<String,Object> condtion) {
        LOGGER.info("SendMessageController.sendMsg（发送短信） params : {} ", JSON.toJSONString(condtion));

        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS
                .getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            String responseText=sendMsgService.sendMsg(condtion);
            result.setData(responseText);
        } catch (BaseException e) {
            LOGGER.error("SendMessageController.sendMsg ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("SendMessageController.sendMsg ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(),
                    null);
        }
        return result;
    }


}
